package com.hzhc.service.impl;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.common.Base64FileUpload;
import com.hzhc.common.FileExcelUtil;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.*;
import com.hzhc.enumBean.FileTypeEnum;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.YwOrderTypeEnum;
import com.hzhc.mapper.*;
import com.hzhc.service.*;
import com.hzhc.utils.EasypoiExcelUtils;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.StringUtils;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.*;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.checkerframework.checker.units.qual.Prefix.one;

/**
 * <p>
 * 车辆预约 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-27
 */
@Service
@RequiredArgsConstructor
public class VehicleReservationServiceImpl extends ServiceImpl<VehicleReservationMapper, VehicleReservation> implements VehicleReservationService {
    private final YwOrderCntrService ywOrderCntrService;
    private final YwOrderCntrMapper ywOrderCntrMapper;
    private final CntrImgFileService cntrImgFileService;
    private final CntrImgFileMapper cntrImgFileMapper;
    private final YwOrderMapper ywOrderMapper;
    private final CntrInfoMapper cntrInfoMapper;
    private final CarInfoService carInfoService;
    private final UserMemberService userMemberService;
    private final DeclareCarMapper declareCarMapper;
    private final VehicleReservationMapper vehicleReservationMapper;
    private final YwOrderCarMapper ywOrderCarMapper;
    private final CntrLogService logService;
    private final CarInfoMapper carInfoMapper;
    private final SysDictMapper dictMapper;
    private final SysAuthMapper sysAuthMapper;
    private final TowInfoMapper towInfoMapper;

    @Override
    public List<VehicleReservationVo> getWxList(Page<VehicleReservationVo> page, VehicleReservationVo vo) {
        IPage<VehicleReservationVo> vehicleReservationIPage = baseMapper.getWxList(page, vo);
        Map<String, String> orderTypeMap = YwOrderTypeEnum.stream()
                .collect(Collectors.toMap(YwOrderTypeEnum::getCode, YwOrderTypeEnum::getName));
//        for (VehicleReservationVo record : vehicleReservationIPage.getRecords()) {
//            Map<String, List<String>> map = new HashMap<>();
//            List<String> fileList = new ArrayList<>();
//            List<String> fileListB = new ArrayList<>();
//            if (record.getDataFile() != null) {
//                for (CntrImgFile cntrImgFile : record.getDataFile()) {
//                    if (cntrImgFile.getFileSubfolder().contains(record.getCntrNo())) {
//                        fileList.add(cntrImgFile.getFileSubfolder() + cntrImgFile.getFileName());
//                    }
//                    if (StrUtil.isNotBlank(record.getCntrNoB()) && cntrImgFile.getFileSubfolder().contains(record.getCntrNoB())) {
//                        fileListB.add(cntrImgFile.getFileSubfolder() + cntrImgFile.getFileName());
//                    }
//                }
//                if (!fileList.isEmpty()) {
//                    map.put("cntrNo", fileList);
//                }
//                if (!fileListB.isEmpty()) {
//                    map.put("cntrNoB", fileListB);
//                }
//            }
//            String orderType = record.getOrderType();
//            // 检查Map中是否有对应的orderType
//            if (orderTypeMap.containsKey(orderType)) {
//                record.setReservationTypeName(orderTypeMap.get(orderType));
//            }
//            record.setFileMap(map);
//        }
        return vehicleReservationIPage.getRecords();
    }

    @Override
    public ResultUtil<T> addVehicleReservation(VehicleReservationVo vo, JSONArray cntrPhoto, JSONArray cntrPhotoB, String upfolder, String accessPath) {
        //限制同一辆车预约
//        List<VehicleReservation> vehicleReservations = baseMapper.selectList(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("car_no", vo.getCarNo()).eq("end_state", 0));
//        if(!vehicleReservations.isEmpty()){
//            return ResultUtil.build(400,"同一辆车不能重复预约");
//        }
        //预约不限制

        //校验车牌号 不存在则新增
        carInfoService.checkCarNo(vo.getCarNo(), vo.getCompanyId());

        //判断车辆是否黑名单
        CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
        if (car.getIsBlack() == 1) {
            return ResultUtil.build(400, "该车为黑名单！");
        }
        //判断司机是否黑名单
        if (vo.getDriver()!=null){
            UserMember member = userMemberService.getById(vo.getDriver());
            if (member.getIsBlack() == 1) {
                return ResultUtil.build(400, "该司机为黑名单！");
            }
        }

        List<VehicleReservation> vehicleReservationListA = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>()
                .and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdA()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdA()))
                .eq(VehicleReservation::getDisable, 0)
                .eq(VehicleReservation::getEndState, 0)
        );
        if (!vehicleReservationListA.isEmpty()) {
            return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
        }
        if (vo.getCntrIdB() != null) {
            List<VehicleReservation> vehicleReservationListB = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>().
                    and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdB()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdB()))
                    .eq(VehicleReservation::getDisable, 0)
                    .eq(VehicleReservation::getEndState, 0)
            );
            if (!vehicleReservationListB.isEmpty()) {
                return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
            }
        }
        vo.setCreateTime(LocalDateTime.now());
        VehicleReservation reservation = new VehicleReservation();
        reservation.setUserId(vo.getUserId());
        BeanUtils.copyProperties(vo, reservation);
        VehicleReservation reservation1 = new VehicleReservation();
        if (YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
            reservation.setTsStatus(0);
            com.hzhc.utils.BeanUtils.copyProperties(reservation, reservation1);
            reservation1.setTsStatus(1);
        }
        boolean b = save(reservation);
        if (b && YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
            reservation1.setVehicleId(reservation.getId());
            save(reservation1);
        }
        //箱清单 设置 预约状态
        List<CntrLog> logList = new ArrayList<>();//箱动态
        YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vo.getCntrIdA());
        ywOrderCntr.setOrderFlag(1);
        ywOrderCntr.setVehicleId(reservation.getId());
        ywOrderCntrMapper.updateById(ywOrderCntr);
        CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr.getCntrNo());//箱A信息
        CntrLog log = new CntrLog();
        BeanUtils.copyProperties(cntrInfo, log);
        log.setAccesspath("PC");
        log.setCreatetime(LocalDateTime.now());
        log.setRemark("非SGJ车辆预约");
        log.setOrderNo(ywOrderCntr.getOrderNo());//委托号
        logList.add(log);
        if (vo.getCntrIdB() != null) {
            YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vo.getCntrIdB());
            ywOrderCntr1.setOrderFlag(1);
            ywOrderCntr1.setVehicleId(reservation.getId());
            ywOrderCntrMapper.updateById(ywOrderCntr1);
            CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(ywOrderCntr1.getCntrNo());//箱B信息
            CntrLog log1 = new CntrLog();
            BeanUtils.copyProperties(cntrInfo1, log1);
            log1.setRemark("非SGJ车辆预约");
            log1.setAccesspath("PC");
            log1.setCreatetime(LocalDateTime.now());
            log1.setOrderNo(ywOrderCntr.getOrderNo());//委托号
            logList.add(log1);
        }
        logService.saveBatch(logList);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> addVehicleReservation(VehicleReservationVo vo, JSONArray cntrPhoto, JSONArray cntrPhotoB) {

        //预约不限制

        //校验车牌号 不存在则新增
        carInfoService.checkCarNo(vo.getCarNo(), vo.getCompanyId());

        //判断车辆是否黑名单
        CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
        if (car.getIsBlack() == 1) {
            return ResultUtil.build(400, "该车为黑名单！");
        }

        //判断司机是否黑名单
        if(vo.getDriver()!=null){
            UserMember member = userMemberService.getById(vo.getDriver());
            if (member.getIsBlack() == 1) {
                return ResultUtil.build(400, "该司机为黑名单！");
            }
        }


        List<VehicleReservation> vehicleReservationListA = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>()
                .and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdA()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdA()))
                .eq(VehicleReservation::getDisable, 0)
                .eq(VehicleReservation::getEndState, 0)
        );
        if (!vehicleReservationListA.isEmpty()) {
            return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
        }
        if (vo.getCntrIdB() != null) {
            List<VehicleReservation> vehicleReservationListB = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>().
                    and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdB()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdB()))
                    .eq(VehicleReservation::getDisable, 0)
                    .eq(VehicleReservation::getEndState, 0)
            );
            if (!vehicleReservationListB.isEmpty()) {
                return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
            }
        }
        vo.setCreateTime(LocalDateTime.now());
        VehicleReservation reservation = new VehicleReservation();
        reservation.setUserId(vo.getUserId());
        BeanUtils.copyProperties(vo, reservation);
        VehicleReservation reservation1 = new VehicleReservation();
        if (YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
            reservation.setTsStatus(0);
            com.hzhc.utils.BeanUtils.copyProperties(reservation, reservation1);
            reservation1.setTsStatus(1);
        }
        boolean b = save(reservation);
        if (b && YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
            reservation1.setVehicleId(reservation.getId());
            save(reservation1);
        }
        //箱清单 设置 预约状态
        List<CntrLog> logList = new ArrayList<>();//箱动态
        YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vo.getCntrIdA());
        ywOrderCntr.setOrderFlag(1);
        ywOrderCntr.setVehicleId(reservation.getId());
        ywOrderCntrMapper.updateById(ywOrderCntr);
        CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr.getCntrNo());//箱A信息
        CntrLog log = new CntrLog();
        BeanUtils.copyProperties(cntrInfo, log);
        log.setAccesspath("PC");
        log.setCreatetime(LocalDateTime.now());
        log.setRemark("非SGJ车辆预约");
        log.setOrderNo(ywOrderCntr.getOrderNo());//委托号
        logList.add(log);
        if (vo.getCntrIdB() != null) {
            YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vo.getCntrIdB());
            ywOrderCntr1.setOrderFlag(1);
            ywOrderCntr1.setVehicleId(reservation.getId());
            ywOrderCntrMapper.updateById(ywOrderCntr1);
            CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(ywOrderCntr1.getCntrNo());//箱B信息
            CntrLog log1 = new CntrLog();
            BeanUtils.copyProperties(cntrInfo1, log1);
            log1.setRemark("非SGJ车辆预约");
            log1.setAccesspath("PC");
            log1.setCreatetime(LocalDateTime.now());
            log1.setOrderNo(ywOrderCntr.getOrderNo());//委托号
            logList.add(log1);
        }
        logService.saveBatch(logList);

        return b ? ResultUtil.ok() : ResultUtil.fail();
    }


    @Nullable
    private ResultUtil<T> addCntrFile(JSONArray cntrPhoto, JSONArray cntrPhotoB, String upfolder, String accessPath, VehicleReservation reservation) {
        //图片上传返回路径
        if (cntrPhoto != null) {
            List<CntrImgFile> cntrImgFiles = new ArrayList<>();
            CntrImgFile cntrImgFile = null;
            for (int i = 0; i < cntrPhoto.size(); i++) {
                cntrImgFile = new CntrImgFile();
                String p = (String) cntrPhoto.get(i);
                Map<String, Object> photoMap = Base64FileUpload.uploadCntrFileBase64(reservation, p, upfolder, reservation.getCntrNo());
                if ((boolean) photoMap.get("success")) {
                    if (photoMap.get("data") != null) {
                        JSONObject objA = (JSONObject) JSON.toJSON(photoMap.get("data"));
                        cntrImgFile.setOrderNo(reservation.getOrderNo()).setFileType(FileTypeEnum.CLYY.getCode())
                                .setFileSubfolder(accessPath.replaceAll("\\*", "") + objA.getString("path") + "/")
                                .setFileName(objA.getString("fileName")).setCreateTime(LocalDateTime.now()).setCreateUser(reservation.getUserName());
                        cntrImgFiles.add(cntrImgFile);
                    }
                } else {
                    return ResultUtil.fail();
                }
            }
            if (!cntrImgFiles.isEmpty()) {
                boolean b1 = cntrImgFileService.saveBatch(cntrImgFiles);
                if (!b1) {
                    return ResultUtil.fail();
                }
            }
        }
        if (cntrPhotoB != null) {
            List<CntrImgFile> cntrImgFiles = new ArrayList<>();
            CntrImgFile cntrImgFile = null;
            for (int i = 0; i < cntrPhotoB.size(); i++) {
                cntrImgFile = new CntrImgFile();
                String p = (String) cntrPhotoB.get(i);
                Map<String, Object> photoMap = Base64FileUpload.uploadCntrFileBase64(reservation, p, upfolder, reservation.getCntrNoB());
                if ((boolean) photoMap.get("success")) {
                    if (photoMap.get("data") != null) {
                        JSONObject objA = (JSONObject) JSON.toJSON(photoMap.get("data"));
                        cntrImgFile.setOrderNo(reservation.getOrderNo()).setFileType(FileTypeEnum.CLYY.getCode())
                                .setFileName(objA.getString("fileName")).setCreateTime(LocalDateTime.now()).setCreateUser(reservation.getUserName());
                        cntrImgFiles.add(cntrImgFile);
                    }
                } else {
                    return ResultUtil.fail();
                }
            }
            if (!cntrImgFiles.isEmpty()) {
                boolean b1 = cntrImgFileService.saveBatch(cntrImgFiles);
                if (!b1) {
                    return ResultUtil.fail();
                }
            }
        }
        return null;
    }

    @Override
    public ResultUtil<T> updateVehicleReservation(VehicleReservationVo vo, JSONArray cntrPhoto, JSONArray cntrPhotoB, String upfolder, String accessPath) {
        //限制同一辆车预约
//        List<VehicleReservation> vehicleReservationList = baseMapper.selectList(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("car_no", vo.getCarNo())
//                .eq("end_state", 0).ne("id", vo.getId()));
//        if(!vehicleReservationList.isEmpty()){
//            return ResultUtil.build(400,"同一辆车不能重复预约");
//        }
        //校验车牌号 不存在则新增
        carInfoService.checkCarNo(vo.getCarNo(), vo.getCompanyId());

        //判断车辆是否黑名单
        CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
        if (car.getIsBlack() == 1) {
            return ResultUtil.build(400, "该车为黑名单！");
        }

        VehicleReservation vehicleReservation = baseMapper.selectById(vo.getId());

        //判断司机是否黑名单
        if (vo.getDriver()!=null){
            UserMember member = userMemberService.getById(vehicleReservation.getDriver());
            if (member.getIsBlack() == 1) {
                return ResultUtil.build(400, "该司机为黑名单！");
            }
        }

        List<VehicleReservation> vehicleReservationListA = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>()
                .and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdA()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdA()))
                .eq(VehicleReservation::getDisable, 0)
                .eq(VehicleReservation::getEndState, 0)
                .ne(VehicleReservation::getId, vo.getId()));
        if (!vehicleReservationListA.isEmpty()) {
            return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
        }
        if (vo.getCntrIdB() != null) {
            List<VehicleReservation> vehicleReservationListB = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>().
                    and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdB()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdB()))
                    .eq(VehicleReservation::getDisable, 0)
                    .eq(VehicleReservation::getEndState, 0)
                    .ne(VehicleReservation::getId, vo.getId()));
            if (!vehicleReservationListB.isEmpty()) {
                return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
            }
        }

        //移除旧数据
        YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdA());
        ywOrderCntr.setOrderFlag(0);
        ywOrderCntr.setVehicleId(null);

        ywOrderCntrMapper.updateByIdOwn(ywOrderCntr);
//        ywOrderCntrMapper.updateById(ywOrderCntr);
        if (vehicleReservation.getCntrIdB() != null) {
            YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdB());
            ywOrderCntr1.setOrderFlag(0);
            ywOrderCntr1.setVehicleId(null);
            ywOrderCntrMapper.updateByIdOwn(ywOrderCntr1);
        }
        vehicleReservation.setCntrIdA(vo.getCntrIdA());
        vehicleReservation.setCntrIdB(vo.getCntrIdB());
        vehicleReservation.setCntrSize(vo.getCntrSize());
        vehicleReservation.setCntrSizeB(vo.getCntrSizeB());
        vehicleReservation.setCntrNo(vo.getCntrNo());
        vehicleReservation.setCntrNoB(vo.getCntrNoB());
        vehicleReservation.setCarNo(vo.getCarNo());
        vehicleReservation.setUpdateMemberId(vo.getUpdateMemberId());
        vehicleReservation.setUpdateUserName(vo.getUpdateUserName());
        vehicleReservation.setDriver(vo.getDriver());
        boolean b = updateById(vehicleReservation);
        if (b) {

            if (b && YwOrderTypeEnum.TZHK.getCode().equals(vehicleReservation.getReservationType())) {
                VehicleReservation vehicleReservation1 = baseMapper.selectOne(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("vehicle_id", vehicleReservation.getId()));
                if (vehicleReservation1 != null) {
                    VehicleReservation reservation = new VehicleReservation();
                    BeanUtils.copyProperties(vehicleReservation, reservation);
                    reservation.setId(vehicleReservation1.getId());
                    reservation.setTsStatus(1);
                    reservation.setVehicleId(vehicleReservation1.getVehicleId());
                    updateById(reservation);
                }
            }

            YwOrderCntr cntr = ywOrderCntrMapper.selectById(vo.getCntrIdA());
            cntr.setOrderFlag(1);
            cntr.setVehicleId(vehicleReservation.getId());

            ywOrderCntrMapper.updateByIdOwn(cntr);

            if (vo.getCntrIdB() != null) {
                YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vo.getCntrIdB());
                ywOrderCntr1.setOrderFlag(1);
                ywOrderCntr1.setVehicleId(vehicleReservation.getId());
                ywOrderCntrMapper.updateByIdOwn(ywOrderCntr1);
            }
//            ResultUtil<T> fail = updateFiles(cntrPhoto, cntrPhotoB, upfolder, accessPath, vehicleReservation);
//            if (fail != null) return fail;
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateVehicleReservation(VehicleReservationVo vo, JSONArray cntrPhoto, JSONArray cntrPhotoB) {
        //校验车牌号 不存在则新增
        carInfoService.checkCarNo(vo.getCarNo(), vo.getCompanyId());

        //判断车辆是否黑名单
        CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
        if (car.getIsBlack() == 1) {
            return ResultUtil.build(400, "该车为黑名单！");
        }
        //判断司机是否黑名单
        if (vo.getDriver()!=null){
            UserMember member = userMemberService.getById(vo.getDriver());
            if (member.getIsBlack() == 1) {
                return ResultUtil.build(400, "该司机为黑名单！");
            }
        }

        VehicleReservation vehicleReservation = baseMapper.selectById(vo.getId());

        List<VehicleReservation> vehicleReservationListA = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>()
                .and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdA()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdA()))
                .eq(VehicleReservation::getDisable, 0)
                .eq(VehicleReservation::getEndState, 0)
                .ne(VehicleReservation::getId, vo.getId()));
        if (!vehicleReservationListA.isEmpty()) {
            return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
        }
        if (vo.getCntrIdB() != null) {
            List<VehicleReservation> vehicleReservationListB = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>().
                    and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vo.getCntrIdB()).or().eq(VehicleReservation::getCntrIdB, vo.getCntrIdB()))
                    .eq(VehicleReservation::getDisable, 0)
                    .eq(VehicleReservation::getEndState, 0)
                    .ne(VehicleReservation::getId, vo.getId()));
            if (!vehicleReservationListB.isEmpty()) {
                return ResultUtil.build(400, "箱号" + vo.getCntrNo() + "已被预约");
            }
        }

        //移除旧数据
        YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdA());
        ywOrderCntr.setOrderFlag(0);
        ywOrderCntr.setVehicleId(null);

        ywOrderCntrMapper.updateByIdOwn(ywOrderCntr);
//        ywOrderCntrMapper.updateById(ywOrderCntr);
        if (vehicleReservation.getCntrIdB() != null) {
            YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdB());
            ywOrderCntr1.setOrderFlag(0);
            ywOrderCntr1.setVehicleId(null);
            ywOrderCntrMapper.updateByIdOwn(ywOrderCntr1);
        }
        vehicleReservation.setCntrIdA(vo.getCntrIdA());
        vehicleReservation.setCntrIdB(vo.getCntrIdB());
        vehicleReservation.setCntrSize(vo.getCntrSize());
        vehicleReservation.setCntrSizeB(vo.getCntrSizeB());
        vehicleReservation.setCntrNo(vo.getCntrNo());
        vehicleReservation.setCntrNoB(vo.getCntrNoB());
        vehicleReservation.setCarNo(vo.getCarNo());
        vehicleReservation.setUpdateMemberId(vo.getUpdateMemberId());
        vehicleReservation.setUpdateUserName(vo.getUpdateUserName());
        vehicleReservation.setDriver(vo.getDriver());
        boolean b = updateById(vehicleReservation);
        if (b) {

            if (b && YwOrderTypeEnum.TZHK.getCode().equals(vehicleReservation.getReservationType())) {
                VehicleReservation vehicleReservation1 = baseMapper.selectOne(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("vehicle_id", vehicleReservation.getId()));
                if (vehicleReservation1 != null) {
                    VehicleReservation reservation = new VehicleReservation();
                    BeanUtils.copyProperties(vehicleReservation, reservation);
                    reservation.setId(vehicleReservation1.getId());
                    reservation.setTsStatus(1);
                    reservation.setVehicleId(vehicleReservation1.getVehicleId());
                    updateById(reservation);
                }
            }

            YwOrderCntr cntr = ywOrderCntrMapper.selectById(vo.getCntrIdA());
            cntr.setOrderFlag(1);
            cntr.setVehicleId(vehicleReservation.getId());

            ywOrderCntrMapper.updateByIdOwn(cntr);

            if (vo.getCntrIdB() != null) {
                YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vo.getCntrIdB());
                ywOrderCntr1.setOrderFlag(1);
                ywOrderCntr1.setVehicleId(vehicleReservation.getId());
                ywOrderCntrMapper.updateByIdOwn(ywOrderCntr1);
            }
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Nullable
    private ResultUtil<T> updateFiles(JSONArray cntrPhoto, JSONArray cntrPhotoB, String upfolder, String accessPath, VehicleReservation reservation) {
        List<CntrImgFile> cntrImgFileList = cntrImgFileMapper.selectList(new QueryWrapper<CntrImgFile>().eq("disable", 0)
                .eq("connect_id", reservation.getId()).eq("file_type", FileTypeEnum.CLYY.getCode()));
        List<CntrImgFile> cntrNoList = cntrImgFileList.stream().filter(s -> s.getFileSubfolder().contains(reservation.getCntrNo())).collect(Collectors.toList());
        List<CntrImgFile> cntrNoBList = cntrImgFileList.stream().filter(s -> s.getFileSubfolder().contains(reservation.getCntrNoB())).collect(Collectors.toList());
        List<CntrImgFile> delList = new ArrayList<>();
        for (CntrImgFile cntrImgFile : cntrNoList) {
            if (cntrPhoto != null) {
                boolean flag = true;
                for (Object o : cntrPhoto) {
                    String p = (String) o;
                    if (cntrImgFile.getFileName().equals(p)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    cntrImgFile.setDisable(1);
                    delList.add(cntrImgFile);
                }
            }
        }
        for (CntrImgFile cntrImgFile : cntrNoBList) {
            if (cntrPhotoB != null) {
                boolean flag = true;
                for (Object o : cntrPhotoB) {
                    String p = (String) o;
                    if (cntrImgFile.getFileName().equals(p)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    cntrImgFile.setDisable(1);
                    delList.add(cntrImgFile);
                }
            }
        }

        cntrImgFileService.updateBatchById(delList);

        //图片上传返回路径
        if (cntrPhoto != null) {
            List<CntrImgFile> cntrImgFiles = new ArrayList<>();
            CntrImgFile cntrImgFile = null;
            for (int i = 0; i < cntrPhoto.size(); i++) {
                cntrImgFile = new CntrImgFile();
                String p = (String) cntrPhoto.get(i);

                if (p.startsWith("data:image")) {
                    Map<String, Object> photoMap = Base64FileUpload.uploadCntrFileBase64(reservation, p, upfolder, reservation.getCntrNo());
                    if ((boolean) photoMap.get("success")) {
                        if (photoMap.get("data") != null) {
                            JSONObject objA = (JSONObject) JSON.toJSON(photoMap.get("data"));
                            cntrImgFile.setOrderNo(reservation.getOrderNo()).setFileType(FileTypeEnum.CLYY.getCode())
                                    .setFileSubfolder(accessPath.replaceAll("\\*", "") + objA.getString("path") + "/")
                                    .setFileName(objA.getString("fileName")).setCreateTime(LocalDateTime.now()).setCreateUser(reservation.getUserName());
                            cntrImgFiles.add(cntrImgFile);
                        }
                    } else {
                        return ResultUtil.fail();
                    }
                }
            }
            if (!cntrImgFiles.isEmpty()) {
                boolean b1 = cntrImgFileService.saveBatch(cntrImgFiles);
                if (!b1) {
                    return ResultUtil.fail();
                }
            }
        }
        if (cntrPhotoB != null) {
            List<CntrImgFile> cntrImgFiles = new ArrayList<>();
            CntrImgFile cntrImgFile = null;
            for (int i = 0; i < cntrPhotoB.size(); i++) {
                cntrImgFile = new CntrImgFile();
                String p = (String) cntrPhotoB.get(i);

                if (p.startsWith("data:image")) {
                    Map<String, Object> photoMap = Base64FileUpload.uploadCntrFileBase64(reservation, p, upfolder, reservation.getCntrNoB());
                    if ((boolean) photoMap.get("success")) {
                        if (photoMap.get("data") != null) {
                            JSONObject objA = (JSONObject) JSON.toJSON(photoMap.get("data"));
                            cntrImgFile.setOrderNo(reservation.getOrderNo()).setFileType(FileTypeEnum.CLYY.getCode())
                                    .setFileSubfolder(accessPath.replaceAll("\\*", "") + objA.getString("path") + "/")
                                    .setFileName(objA.getString("fileName")).setCreateTime(LocalDateTime.now()).setCreateUser(reservation.getUserName());
                            cntrImgFiles.add(cntrImgFile);
                        }
                    } else {
                        return ResultUtil.fail();
                    }
                }
            }
            if (!cntrImgFiles.isEmpty()) {
                boolean b1 = cntrImgFileService.saveBatch(cntrImgFiles);
                if (!b1) {
                    return ResultUtil.fail();
                }
            }
        }
        return null;
    }

    @Override
    public ResultUtil<T> delVehicleReservation(VehicleReservation vo) {
        VehicleReservation vehicleReservation = baseMapper.selectById(vo.getId());
        vehicleReservation.setDisable(1);
        YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdA());
        List<CntrLog> logList = new ArrayList<>();//箱动态
        if (ywOrderCntr != null) {
            if (ywOrderCntr.getTallyState() != 0) {
                return ResultUtil.build(400, "箱已理货，无法删除");
            }
            CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr.getCntrNo());
            CntrLog log = new CntrLog();
            BeanUtils.copyProperties(cntrInfo, log);
            log.setAccesspath("PC");
            log.setCreatetime(LocalDateTime.now());
            log.setRemark("车辆预约取消");
            log.setOrderNo(ywOrderCntr.getOrderNo());//委托号
            logList.add(log);
            ywOrderCntr.setOrderFlag(0);
            ywOrderCntr.setVehicleId(null);
            ywOrderCntrMapper.updateById(ywOrderCntr);
        }
        if (vehicleReservation.getCntrIdB() != null) {
            YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdB());
            if (ywOrderCntr1 != null) {
                if (ywOrderCntr1.getTallyState() != 0) {
                    return ResultUtil.build(400, "箱已理货，无法删除");
                }
                CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr1.getCntrNo());
                CntrLog log = new CntrLog();
                BeanUtils.copyProperties(cntrInfo, log);
                log.setAccesspath("PC");
                log.setCreatetime(LocalDateTime.now());
                log.setRemark("车辆预约取消");
                log.setOrderNo(ywOrderCntr1.getOrderNo());//委托号
                logList.add(log);
                ywOrderCntr1.setOrderFlag(0);
                ywOrderCntr1.setVehicleId(null);
                ywOrderCntrMapper.updateById(ywOrderCntr1);
            }
        }
        logService.saveBatch(logList);
//        List<CntrImgFile> cntrImgFiles = cntrImgFileMapper.selectList(new QueryWrapper<CntrImgFile>().eq("disable", 0)
//                .eq("connect_id", vehicleReservation.getId()).eq("file_type", FileTypeEnum.CLYY.getCode()));
//        if (!cntrImgFiles.isEmpty()) {
//            for (CntrImgFile cntrImgFile : cntrImgFiles) {
//                cntrImgFile.setDisable(1);
//            }
//            cntrImgFileService.updateBatchById(cntrImgFiles);
//        }
        boolean b = updateById(vehicleReservation);
        if (b && YwOrderTypeEnum.TZHK.getCode().equals(vehicleReservation.getReservationType())) {
            VehicleReservation vehicleReservation1 = baseMapper.selectOne(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("vehicle_id", vehicleReservation.getId()));
            if (vehicleReservation1 != null) {
                vehicleReservation1.setDisable(1);
                updateById(vehicleReservation1);
            }
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public List<VehicleReservationVo> getVehicleReservationList(Page<VehicleReservationVo> page, VehicleReservationVo vo) {
        IPage<VehicleReservationVo> vehicleReservationIPage = baseMapper.getVehicleReservationList(page, vo);
//        for (VehicleReservationVo record : vehicleReservationIPage.getRecords()) {
//            Map<String, List<String>> map = new HashMap<>();
//            List<String> fileList = new ArrayList<>();
//            List<String> fileListB = new ArrayList<>();
//            if (record.getDataFile() != null) {
//                for (CntrImgFile cntrImgFile : record.getDataFile()) {
//                    if (cntrImgFile.getFileSubfolder().contains(record.getCntrNo())) {
//                        fileList.add(cntrImgFile.getFileSubfolder() + cntrImgFile.getFileName());
//                    }
//                    if (StrUtil.isNotBlank(record.getCntrNoB()) && cntrImgFile.getFileSubfolder().contains(record.getCntrNoB())) {
//                        fileListB.add(cntrImgFile.getFileSubfolder() + cntrImgFile.getFileName());
//                    }
//                }
//                if (!fileList.isEmpty()) {
//                    map.put("cntrNo", fileList);
//                }
//                if (!fileListB.isEmpty()) {
//                    map.put("cntrNoB", fileListB);
//                }
//            }
//            record.setFileMap(map);
//        }
        return vehicleReservationIPage.getRecords();
    }

    @Override
    public ResultUtil<T> addTkzhVehicleReservation(VehicleReservationVo vo) {
        vo.setReservationType(YwOrderTypeEnum.TK.getCode());
        carInfoService.checkCarNo(vo.getCarNo(), vo.getCompanyId());
        //限制同一辆车预约
//        List<VehicleReservation> vehicleReservations = baseMapper.selectList(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("car_no", vo.getCarNo())
//                .eq("end_state", 0));
//        if(!vehicleReservations.isEmpty()){
//            return ResultUtil.build(400,"同一辆车不能重复预约");
//        }
        //判断车辆是否黑名单
        CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
        if (car.getIsBlack() == 1) {
            return ResultUtil.build(400, "该车为黑名单！");
        }
        //判断司机是否黑名单
        UserMember member = userMemberService.getById(vo.getDriver());
        if (member.getIsBlack() == 1) {
            return ResultUtil.build(400, "该司机为黑名单！");
        }
        List<YwOrderCntr> ywOrderCntrs = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>().like("cntr_size", vo.getCntrSize())
                .eq("disable", 0).eq("order_flag", 0).eq("order_no", vo.getOrderNo()));
        List<YwOrderCntr> saveList = new ArrayList<>();
        if (!ywOrderCntrs.isEmpty()) {
            if (ywOrderCntrs.size() < vo.getCntrNum()) {
                return ResultUtil.build(400, "箱子数量不足,请检查!");
            } else {
                for (int i = 0; i < vo.getCntrNum(); i++) {
                    saveList.add(ywOrderCntrs.get(i));
                    if (vo.getCntrIdA() != null) {
                        vo.setCntrIdB(ywOrderCntrs.get(i).getId());
                        vo.setCntrSizeB(ywOrderCntrs.get(i).getCntrSize());
                    } else {
                        vo.setCntrIdA(ywOrderCntrs.get(i).getId());
                        vo.setCntrSize(ywOrderCntrs.get(i).getCntrSize());
                    }
                }
            }
        } else {
            return ResultUtil.build(400, "无可用箱子预约,请检查!");
        }
        vo.setCreateTime(LocalDateTime.now());
        VehicleReservation reservation = new VehicleReservation();
        BeanUtils.copyProperties(vo, reservation);
        boolean b = save(reservation);
        List<CntrLog> logList = new ArrayList<>();//箱动态
        //箱清单 设置 预约状态
        for (YwOrderCntr cntr : saveList) {
            cntr.setOrderFlag(1);
            cntr.setVehicleId(reservation.getId());
            CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(cntr.getCntrNo());//箱信息
            CntrLog log = new CntrLog();
            BeanUtils.copyProperties(cntrInfo, log);
            log.setAccesspath("PC");
            log.setCreatetime(LocalDateTime.now());
            log.setRemark("SGJ车辆预约");
            log.setOrderNo(cntr.getOrderNo());//委托号
            logList.add(log);
        }
        logService.saveBatch(logList);
        ywOrderCntrService.updateBatchById(saveList);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateTkzhVehicleReservation(VehicleReservationVo vo) {
        carInfoService.checkCarNo(vo.getCarNo(), vo.getCompanyId());
        //限制同一辆车预约
        List<VehicleReservation> vehicleReservations = baseMapper.selectList(new QueryWrapper<VehicleReservation>().eq("disable", 0).eq("car_no", vo.getCarNo())
                .eq("end_state", 0).ne("id", vo.getId()));
        if (!vehicleReservations.isEmpty()) {
            return ResultUtil.build(400, "同一辆车不能重复预约");
        }

        //清空原先预约数据
        List<YwOrderCntr> ywOrderCntrList = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>()
                .eq("disable", 0).eq("vehicle_id", vo.getId()));
        if (!ywOrderCntrList.isEmpty()) {
            for (YwOrderCntr ywOrderCntr : ywOrderCntrList) {
                ywOrderCntr.setVehicleId(null);
                ywOrderCntr.setOrderFlag(0);
            }
        }
        ywOrderCntrService.updateBatchById(ywOrderCntrList);

        List<YwOrderCntr> ywOrderCntrs = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>().eq("cntr_size", vo.getCntrSize())
                .eq("disable", 0).eq("order_flag", 0));
        List<YwOrderCntr> saveList = new ArrayList<>();
        vo.setCntrIdA(null);
        vo.setCntrIdB(null);
        if (!ywOrderCntrs.isEmpty()) {
            if (ywOrderCntrs.size() < vo.getCntrNum()) {
                return ResultUtil.build(400, "箱子数量不足,请检查!");
            } else {
                for (int i = 0; i < vo.getCntrNum(); i++) {
                    ywOrderCntrs.get(i).setVehicleId(vo.getId());
                    ywOrderCntrs.get(i).setOrderFlag(1);
                    saveList.add(ywOrderCntrs.get(i));
                    if (vo.getCntrIdA() != null) {
                        vo.setCntrIdB(ywOrderCntrs.get(i).getId());
                        vo.setCntrSizeB(vo.getCntrSize());
                    } else {
                        vo.setCntrIdA(ywOrderCntrs.get(i).getId());
                        vo.setCntrSize(vo.getCntrSize());
                        vo.setCntrSizeB(null);
                    }
                }
            }
        } else {
            return ResultUtil.build(400, "无可用箱子预约,请检查!");
        }
        vo.setUpdateTime(LocalDateTime.now());
        VehicleReservation reservation = new VehicleReservation();
        BeanUtils.copyProperties(vo, reservation);
        boolean b = updateById(reservation);
        ywOrderCntrService.updateBatchById(saveList);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> delTkzhVehicleReservation(VehicleReservation vo) {
        VehicleReservation reservation = baseMapper.selectById(vo.getId());
        List<YwOrderCntr> ywOrderCntrs = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>()
                .eq("disable", 0).eq("vehicle_id", vo.getId()));
        List<CntrLog> logList = new ArrayList<>();
        if (!ywOrderCntrs.isEmpty()) {
            for (YwOrderCntr ywOrderCntr : ywOrderCntrs) {
                if (ywOrderCntr.getTallyState() != 0) {
                    return ResultUtil.build(400, "箱已理货，无法删除");
                }
                CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr.getCntrNo());
                CntrLog log = new CntrLog();
                BeanUtils.copyProperties(cntrInfo, log);
                log.setAccesspath("PC");
                log.setCreatetime(LocalDateTime.now());
                log.setRemark("车辆预约取消");
                log.setOrderNo(ywOrderCntr.getOrderNo());
                logList.add(log);
                ywOrderCntr.setVehicleId(null);
                ywOrderCntr.setOrderFlag(0);

            }
        }
        logService.saveBatch(logList);
        reservation.setDisable(1);
        boolean b = updateById(reservation);
        ywOrderCntrService.updateBatchById(ywOrderCntrs);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public Map<String, Object> getChooseData(VehicleReservationVo vo) {
        Map<String, Object> map = new HashMap<>();
        List<VehicleReservation> vehicleReservations = baseMapper.getVeficleList(vo);
        if (!vehicleReservations.isEmpty()) {
            YwOrder ywOrder = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable", 0).eq("order_no", vehicleReservations.get(0).getOrderNo()));
            if (vehicleReservations.get(0).getCntrIdA() != null) {
                YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vehicleReservations.get(0).getCntrIdA());
                if (ywOrderCntr != null) {
                    List<CntrInfo> cntrInfos = cntrInfoMapper.selectList(new QueryWrapper<CntrInfo>().eq("disable", 0)
                            .eq("cntr_no", ywOrderCntr.getCntrNo()).eq("status", 0));
                    if (!cntrInfos.isEmpty()) {
                        ywOrderCntr.setBayA(cntrInfos.get(0).getBay());
                    }
                    map.put("cntrA", ywOrderCntr);
                }
            }
            if (vehicleReservations.get(0).getCntrIdB() != null) {
                YwOrderCntr ywOrderCntrB = ywOrderCntrMapper.selectById(vehicleReservations.get(0).getCntrIdB());
                if (ywOrderCntrB != null) {
                    List<CntrInfo> cntrInfos = cntrInfoMapper.selectList(new QueryWrapper<CntrInfo>().eq("disable", 0)
                            .eq("cntr_no", ywOrderCntrB.getCntrNo()).eq("status", 0));
                    if (!cntrInfos.isEmpty()) {
                        ywOrderCntrB.setBayB(cntrInfos.get(0).getBay());
                    }
                    map.put("cntrB", ywOrderCntrB);
                }
            }
            map.put("vehicle", vehicleReservations.get(0));
        }
        return map;
    }

    @Override
    public List<VehicleReservationVo> getYzsVehicleReservationList(Page<VehicleReservationVo> page, VehicleReservationVo vo) {
        IPage<VehicleReservationVo> vehicleReservationIPage = baseMapper.getYzsVehicleReservationList(page, vo);
        return vehicleReservationIPage.getRecords();
    }

    @Override
    public ResultUtil<T> updateSubmitType(UserDto user, Long id) {
        VehicleReservation reservation = getById(id);
        if (reservation.getSubmitType() == 1) {
            return ResultUtil.build(400, "该预约信息已经提交");
        }
        //进  提   只能存在一个 (针对司机)
        String flag = "";
        if ("ZJ".equals(reservation.getReservationType()) || "KJ".equals(reservation.getReservationType())) {//进
            flag = "J";
        } else {//提
            flag = "T";
        }

        LambdaQueryWrapper<VehicleReservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(VehicleReservation::getReservationType, "%" + flag + "%")//业务类型
                .eq(VehicleReservation::getCarNo, reservation.getCarNo())//司机
                .eq(VehicleReservation::getDisable, 0)
                .ne(VehicleReservation::getId, id)
                .eq(VehicleReservation::getSubmitType, 1)//已提交
                .eq(VehicleReservation::getEndState, 0)//未进卡口(业务未完成)
                .ne(VehicleReservation::getReservationType, "SGJ");
        int count = count(wrapper);

        if (count > 0) {
            return ResultUtil.build(400, "该车已提交了同类型的预约");
        }

        reservation.setSubmitType(1);
        boolean b = updateById(reservation);
        //箱动态
        List<CntrLog> logList = new ArrayList<>();
        CntrLog log = new CntrLog();
        CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(reservation.getCntrNo());
        BeanUtils.copyProperties(cntrInfo, log);
        log.setAccesspath("PC");
        log.setCreatetime(LocalDateTime.now());
        log.setUserId(user.getId());
        log.setUserName(user.getRealname());
        log.setOrderNo(reservation.getOrderNo());//委托号
        log.setRemark("车辆预约司机确认");
        logList.add(log);
        if (reservation.getCntrNoB() != null && !"".equals(reservation.getCntrNoB())) {
            CntrLog log1 = new CntrLog();
            CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(reservation.getCntrNoB());
            BeanUtils.copyProperties(cntrInfo1, log1);
            log1.setRemark("车辆预约司机确认");
            log1.setAccesspath("PC");
            log1.setUserId(user.getId());
            log1.setUserName(user.getRealname());
            log1.setCreatetime(LocalDateTime.now());
            log1.setOrderNo(reservation.getOrderNo());//委托号
            logList.add(log1);
        }
        logService.saveBatch(logList);
        if (b) {
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> backSubmitType(UserDto user, Long id) {
        VehicleReservation reservation = getById(id);
        if (reservation.getTallyFlag() != 0 || reservation.getTallyFlagB() != 0) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_TALLY_YET);
        }
        reservation.setSubmitType(0);
        boolean b = updateById(reservation);
        if (b) {
            //箱动态
            List<CntrLog> logList = new ArrayList<>();
            CntrLog log = new CntrLog();
            CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(reservation.getCntrNo());
            BeanUtils.copyProperties(cntrInfo, log);
            log.setAccesspath("PC");
            log.setCreatetime(LocalDateTime.now());
            log.setUserId(user.getId());
            log.setUserName(user.getRealname());
            log.setOrderNo(reservation.getOrderNo());//委托号
            log.setRemark("司机确认回退");
            logList.add(log);
            if (reservation.getCntrNoB() != null && !"".equals(reservation.getCntrNoB())) {
                CntrLog log1 = new CntrLog();
                CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(reservation.getCntrNoB());
                BeanUtils.copyProperties(cntrInfo1, log1);
                log1.setRemark("司机确认回退");
                log1.setAccesspath("PC");
                log1.setUserId(user.getId());
                log1.setUserName(user.getRealname());
                log1.setCreatetime(LocalDateTime.now());
                log1.setOrderNo(reservation.getOrderNo());//委托号
                logList.add(log1);
            }
            logService.saveBatch(logList);
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public List<VehicleReservationVo> getDriverList(Page<VehicleReservationVo> page, VehicleReservationVo vo) {
        IPage<VehicleReservationVo> iPage = baseMapper.getDriverList(page, vo);
        return iPage.getRecords();
    }

    @Override
    public Map<String, Object> getChooseDataByCarNo(VehicleReservationVo vo) {
        Map<String, Object> map = new HashMap<>();
         List<DeclareCar> declareCars = declareCarMapper.selectList(new QueryWrapper<DeclareCar>().eq("car_no", vo.getCarNo()).eq("disable", 0)
                .eq("out_type", 0));
        if (declareCars.size() > 0) {
            if(declareCars.size()>1){
                map.put("declareCar", declareCars.stream().filter(s -> s.getPid()!=null).findFirst().get());
            }else{
                map.put("declareCar", declareCars.get(0));
            }
        }
        //显示提箱委托
        List<YwOrder> txOrder=ywOrderMapper.getTxOrder(vo);
        map.put("txOrder", txOrder);

        CarInfo carInfo = carInfoMapper.selectOne(new QueryWrapper<CarInfo>().eq("car_no", vo.getCarNo()).eq("disable", 0));
        if(carInfo!=null &&  carInfo.getTowId()!=null){
            TowInfo towInfo = towInfoMapper.selectById(carInfo.getTowId());
            if(StringUtils.isNotEmpty(carInfo.getVehicleWt())){
                carInfo.setCarWeight((new BigDecimal(carInfo.getVehicleWt()).add(new BigDecimal(towInfo.getTowWeight()))).divide(new BigDecimal(1000),2,RoundingMode.HALF_UP).toString());
            }else{
                carInfo.setCarWeight(new BigDecimal(towInfo.getTowWeight()).divide(new BigDecimal(1000),2,RoundingMode.HALF_UP).toString());
            }
        }
        map.put("carInfo",carInfo);
        List<YwOrderVo> ywOrders = ywOrderCarMapper.getSgjYwOrderList(vo);
        map.put("sgjYworder", ywOrders);
        return map;
    }

    @Override
    public List<VehicleReservationVo> getVehicleList(Page<VehicleReservationVo> page, VehicleReservationVo vo) {
        IPage<VehicleReservationVo> vehicleReservationIPage = vehicleReservationMapper.getVehicleList(page, vo);
        Map<String, String> orderTypeMap = YwOrderTypeEnum.stream()
                .collect(Collectors.toMap(YwOrderTypeEnum::getCode, YwOrderTypeEnum::getName));

        for (VehicleReservationVo record : vehicleReservationIPage.getRecords()) {
            String orderType = record.getReservationType();
            // 检查Map中是否有对应的orderType
            if (orderTypeMap.containsKey(orderType)) {
                record.setOrderTypeName(orderTypeMap.get(orderType));
            }
        }
        return vehicleReservationIPage.getRecords();
    }

    @Override
    public VehicleReservationVo getXcZtMsg(VehicleReservationVo vo) {
        return baseMapper.selectMsgByCntr(vo.getCntrNo());
    }

    @Override
    public ResultUtil<T> updateCarNo(VehicleReservationVo vo) {
        VehicleReservation reservation = baseMapper.selectById(vo.getId());
        List<CarInfoVo> carList = carInfoService.getCarListByVeh(new CarInfoVo());

        if(carList.size()>0){
            List<String> carNoList = carList.stream().map(CarInfoVo::getCarNo).collect(Collectors.toList());
            if(!(carNoList.contains(vo.getCarNo()))){
                return ResultUtil.build(400, "车牌号不存在");
            }
        }

        boolean b = false;
        if (reservation != null) {
            reservation.setCarNo(vo.getCarNo());
            b = updateById(reservation);
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> importYuyueList(HttpServletRequest request, VehicleReservationVo vo) {
        String orderNo = vo.getOrderNo();
        List<VehicleReservation> yuyueList = vo.getYuyueList();
//        for (VehicleReservation reservation:yuyueList){
//            //校验车牌号 不存在则新增
//            carInfoService.checkCarNo(reservation.getCarNo(),reservation.getCompanyId());
//
//            //判断车辆是否黑名单
//            CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
//            if (car.getIsBlack()==1){
//                return ResultUtil.build(400,"该车为黑名单！");
//            }
//            //判断司机是否黑名单
//            UserMember member = userMemberService.getById(vo.getDriver());
//            if (member.getIsBlack()==1){
//                return ResultUtil.build(400,"该司机为黑名单！");
//            }
//        }
        for (VehicleReservation vehicleReservation : yuyueList) {

            vehicleReservation.setOrderNo(orderNo);
            vehicleReservation.setUserName(vo.getUserName());
            vehicleReservation.setMemberId(vo.getId());
            vehicleReservation.setUserId(vo.getUserId());

            List<VehicleReservation> vehicleReservationListA = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>()
                    .and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vehicleReservation.getCntrIdA()).or().eq(VehicleReservation::getCntrIdB, vehicleReservation.getCntrIdA()))
                    .eq(VehicleReservation::getDisable, 0)
                    .eq(VehicleReservation::getEndState, 0)
            );
            if (!vehicleReservationListA.isEmpty()) {
                return ResultUtil.build(400, "箱号" + vehicleReservation.getCntrNo() + "已被预约");
            }
            if (vehicleReservation.getCntrIdB() != null) {
                List<VehicleReservation> vehicleReservationListB = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>().
                        and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vehicleReservation.getCntrIdB()).or().eq(VehicleReservation::getCntrIdB, vehicleReservation.getCntrIdB()))
                        .eq(VehicleReservation::getDisable, 0)
                        .eq(VehicleReservation::getEndState, 0)
                );
                if (!vehicleReservationListB.isEmpty()) {
                    return ResultUtil.build(400, "箱号" + vehicleReservation.getCntrNo() + "已被预约");
                }
            }
            vehicleReservation.setCreateTime(LocalDateTime.now());
            VehicleReservation reservation = new VehicleReservation();
            reservation.setUserId(vehicleReservation.getUserId());
            BeanUtils.copyProperties(vehicleReservation, reservation);
            VehicleReservation reservation1 = new VehicleReservation();
            if (YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
                reservation.setTsStatus(0);
                com.hzhc.utils.BeanUtils.copyProperties(reservation, reservation1);
                reservation1.setTsStatus(1);
            }
            boolean b = save(reservation);
            if (b && YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
                reservation1.setVehicleId(reservation.getId());
                save(reservation1);
            }
            //箱清单 设置 预约状态
            List<CntrLog> logList = new ArrayList<>();//箱动态
            YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdA());
            ywOrderCntr.setOrderFlag(1);
            ywOrderCntr.setVehicleId(reservation.getId());
            ywOrderCntrMapper.updateById(ywOrderCntr);
            CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr.getCntrNo());//箱A信息
            CntrLog log = new CntrLog();
            BeanUtils.copyProperties(cntrInfo, log);
            log.setAccesspath("PC");
            log.setCreatetime(LocalDateTime.now());
            log.setRemark("非SGJ车辆预约");
            log.setOrderNo(orderNo);//委托号
            logList.add(log);
            if (reservation.getCntrIdB() != null) {
                YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(reservation.getCntrIdB());
                ywOrderCntr1.setOrderFlag(1);
                ywOrderCntr1.setVehicleId(reservation.getId());
                ywOrderCntrMapper.updateById(ywOrderCntr1);
                CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(ywOrderCntr1.getCntrNo());//箱B信息
                CntrLog log1 = new CntrLog();
                BeanUtils.copyProperties(cntrInfo1, log1);
                log1.setRemark("非SGJ车辆预约");
                log1.setAccesspath("PC");
                log1.setCreatetime(LocalDateTime.now());
                log1.setOrderNo(orderNo);//委托号
                logList.add(log1);
            }
            logService.saveBatch(logList);
//            return b ? ResultUtil.ok() : ResultUtil.fail();
        }
        return ResultUtil.ok();
    }

    @Override
    public void exportYuyueFile(HttpServletRequest request, HttpServletResponse response, String exportExcelPathCms) {
        Map<String, Object> map = new HashMap<>();
        TemplateExportParams params = new TemplateExportParams(exportExcelPathCms);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        FileExcelUtil.downloadExcelIo(workbook, request, response, "预约车辆");
    }

    @Override
    public ResultUtil<List<VehicleReservationVo>> YuyueUpload(UserDto user, MultipartFile file, String orderNo, String companyId) {
        List<VehicleReservationVo> list = new ArrayList<VehicleReservationVo>();
        ResultUtil<List<VehicleReservationVo>> build = new ResultUtil<>();
        if (file != null) {
            try {
                List<VehicleReservationVo> planCarVos = EasypoiExcelUtils.importExcel(file, 1, 1, VehicleReservationVo.class);
//                QueryWrapper<VehicleReservation> queryWrapper=new QueryWrapper<>();
//                queryWrapper.eq("disable",0);
//                List<VehicleReservation> yuyueInfos = baseMapper.selectYuyueinfos();
                if (planCarVos.size() == 0) {
                    VehicleReservationVo vo = new VehicleReservationVo();
                    vo.setErrorInfo("文件不能为空！");
                    list.add(vo);
                }
                //字典--流向
                List<SysDictVo> flow = dictMapper.getDictByCode("flow");
                SysDictVo dict = dictMapper.getFlowIdByName("flow","其他");

                for (VehicleReservationVo c : planCarVos) { //校验数据
                    if (ObjectUtil.isEmpty(c)) {
                        c.setErrorInfo("文件中有空数据;");
                        break;
                    }

                    String errorInfo = "";

                    YwOrder order = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable", 0).eq("order_no", orderNo));
                    if (YwOrderTypeEnum.TZ.getCode().equals(order.getOrderType()) || YwOrderTypeEnum.TZHK.getCode().equals(order.getOrderType())) {
                        if (StrUtil.isNotBlank(c.getCntrNo()) && StrUtil.isNotBlank(c.getCntrNoB())) {
                            errorInfo += "到达业务只能选择一个箱子;";
                        }
                    }

                    if (!StrUtil.isNotBlank(c.getCarNo())) {
                        errorInfo += "车牌号未填;";
                    }
//                    if (!StrUtil.isNotBlank(c.getDriverName())) {
//                        errorInfo += "司机信息未填;";
//                    }
//                    if (!StrUtil.isNotBlank(c.getTelephone())) {
//                        errorInfo += "手机号未填;";
//                    }
                    if (StrUtil.isBlank(c.getCntrNo())) {
                        errorInfo += "箱号未填;";
                    }
                    //流向校验
                    boolean flag = true;
                    if (orderNo.startsWith("TZ")){
                        if (StrUtil.isBlank(c.getFlowName())) {
                            errorInfo += "流向未填;";
                        }else{
                            for (SysDictVo sysDictVo : flow) {
                                if (sysDictVo.getName().equals(c.getFlowName())){
                                    flag = false;
                                    c.setFlow(sysDictVo.getId().intValue());
                                }
                            }
                            if (flag){
                                c.setFlow(dict.getId().intValue());
                            }
                        }
                    }
                    errorInfo += checkYuyue(c, orderNo, companyId);
                    c.setErrorInfo(errorInfo);
//                    // 填充 yuyueList 属性
//                    List<VehicleReservation> matchedCarInfos = yuyueInfos.stream()
//                            .filter(e -> e.getCntrNo().equals(c.getCntrNo()))
//                            .collect(Collectors.toList());
//
//                    c.setYuyueList(matchedCarInfos);
                    c.setCompanyId(Long.parseLong(companyId));
                    list.add(c);
                }
                build = ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return build;
    }

    @Override
    public ResultUtil<T> importYZSYuyueList(HttpServletRequest request, VehicleReservationVo vo) {
        String orderNo = vo.getOrderNo();

        List<VehicleReservation> yuyueList = vo.getYuyueList();

//        for (VehicleReservation reservation : yuyueList){
//            //校验车牌号 不存在则新增
//            carInfoService.checkCarNo(reservation.getCarNo(),reservation.getCompanyId());
//            //判断车辆是否黑名单
//            CarInfo car = carInfoService.getByCarNo(reservation.getCarNo());
//            if (car.getIsBlack()==1){
//                return ResultUtil.build(400,"该车为黑名单！");
//            }
//            //判断司机是否黑名单
//            UserMember member = userMemberService.getById(reservation.getDriver());
//            if (member.getIsBlack()==1){
//                return ResultUtil.build(400,"该司机为黑名单！");
//            }
//        }
        for (VehicleReservation vehicleReservation : yuyueList) {

            vehicleReservation.setOrderNo(orderNo);
            vehicleReservation.setUserName(vo.getUserName());
            vehicleReservation.setMemberId(vo.getMemberId());
            vehicleReservation.setCompanyId(vo.getCompanyId());

            List<VehicleReservation> vehicleReservationListA = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>()
                    .and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vehicleReservation.getCntrIdA()).or().eq(VehicleReservation::getCntrIdB, vehicleReservation.getCntrIdA()))
                    .eq(VehicleReservation::getDisable, 0)
                    .eq(VehicleReservation::getEndState, 0)
            );
            if (!vehicleReservationListA.isEmpty()) {
                return ResultUtil.build(400, "箱号" + vehicleReservation.getCntrNo() + "已被预约");
            }
            if (vehicleReservation.getCntrIdB() != null) {
                List<VehicleReservation> vehicleReservationListB = baseMapper.selectList(new LambdaQueryWrapper<VehicleReservation>().
                        and(wrapper -> wrapper.eq(VehicleReservation::getCntrIdA, vehicleReservation.getCntrIdB()).or().eq(VehicleReservation::getCntrIdB, vehicleReservation.getCntrIdB()))
                        .eq(VehicleReservation::getDisable, 0)
                        .eq(VehicleReservation::getEndState, 0)
                );
                if (!vehicleReservationListB.isEmpty()) {
                    return ResultUtil.build(400, "箱号" + vehicleReservation.getCntrNo() + "已被预约");
                }
            }
            vehicleReservation.setCreateTime(LocalDateTime.now());
            VehicleReservation reservation = new VehicleReservation();
            reservation.setUserId(vehicleReservation.getUserId());
            BeanUtils.copyProperties(vehicleReservation, reservation);
            VehicleReservation reservation1 = new VehicleReservation();
            if (YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
                reservation.setTsStatus(0);
                com.hzhc.utils.BeanUtils.copyProperties(reservation, reservation1);
                reservation1.setTsStatus(1);
            }
            boolean b = save(reservation);
            if (b && YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())) {
                reservation1.setVehicleId(reservation.getId());
                save(reservation1);
            }
            //箱清单 设置 预约状态
            List<CntrLog> logList = new ArrayList<>();//箱动态
            YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdA());
            ywOrderCntr.setOrderFlag(1);
            ywOrderCntr.setVehicleId(reservation.getId());
            ywOrderCntrMapper.updateById(ywOrderCntr);
            CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(ywOrderCntr.getCntrNo());//箱A信息
            CntrLog log = new CntrLog();
            BeanUtils.copyProperties(cntrInfo, log);
            log.setAccesspath("PC");
            log.setCreatetime(LocalDateTime.now());
            log.setRemark("非SGJ车辆预约");
            log.setOrderNo(orderNo);//委托号
            logList.add(log);
            if (vehicleReservation.getCntrIdB() != null) {
                YwOrderCntr ywOrderCntr1 = ywOrderCntrMapper.selectById(vehicleReservation.getCntrIdB());
                ywOrderCntr1.setOrderFlag(1);
                ywOrderCntr1.setVehicleId(reservation.getId());
                ywOrderCntrMapper.updateById(ywOrderCntr1);
                CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(ywOrderCntr1.getCntrNo());//箱B信息
                CntrLog log1 = new CntrLog();
                BeanUtils.copyProperties(cntrInfo1, log1);
                log1.setRemark("非SGJ车辆预约");
                log1.setAccesspath("PC");
                log1.setCreatetime(LocalDateTime.now());
                log1.setOrderNo(orderNo);//委托号
                logList.add(log1);
            }
            logService.saveBatch(logList);
//            return b ? ResultUtil.ok() : ResultUtil.fail();
        }
        return ResultUtil.ok();
    }

    @Override
    public void exportYZSYuyueFile(HttpServletRequest request, HttpServletResponse response, String exportExcelPathYZS) {
        Map<String, Object> map = new HashMap<>();
        TemplateExportParams params = new TemplateExportParams(exportExcelPathYZS);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        FileExcelUtil.downloadExcelIo(workbook, request, response, "预约车辆");
    }

    @Override
    public ResultUtil<List<VehicleReservationVo>> YuyueYZSUpload(UserDto user, MultipartFile file, String orderNo, String companyId) {
        List<VehicleReservationVo> list = new ArrayList<VehicleReservationVo>();
        List<String> carList = new ArrayList<String>();
        ResultUtil<List<VehicleReservationVo>> build = new ResultUtil<>();
        if (file != null) {
            try {
                List<VehicleReservationVo> planCarVos = EasypoiExcelUtils.importExcel(file, 1, 1, VehicleReservationVo.class);
//                QueryWrapper<VehicleReservation> queryWrapper=new QueryWrapper<>();
//                queryWrapper.eq("disable",0);
//                List<VehicleReservation> yuyueInfos = baseMapper.selectYuyueinfos();
                //流向
                List<SysDictVo> flowlist = dictMapper.getDictByCode("flow");
                SysDictVo dict = dictMapper.getFlowIdByName("flow", "其他");
                for (VehicleReservationVo c : planCarVos) { //校验数据
                    if (ObjectUtil.isEmpty(c)) {
                        c.setErrorInfo("文件中有空数据;");
                        break;
                    }

                    String errorInfo = "";

                    YwOrder order = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable", 0).eq("order_no", orderNo));
                    if (YwOrderTypeEnum.TZ.getCode().equals(order.getOrderType()) || YwOrderTypeEnum.TZHK.getCode().equals(order.getOrderType())) {
                        if (StrUtil.isNotBlank(c.getCntrNo()) && StrUtil.isNotBlank(c.getCntrNoB())) {
                            errorInfo += "到达业务只能选择一个箱子;";
                        }
                    }
                    if (!StrUtil.isNotBlank(c.getCarNo())) {
                        errorInfo += "车牌号未填;";
                    }
                    if (!StrUtil.isNotBlank(c.getCntrNo())) {
                        errorInfo += "箱号1未填;";
                    }
//                    if (!StrUtil.isNotBlank(c.getDriverName())) {
//                        errorInfo += "司机信息未填;";
//                    }
//                    if (!StrUtil.isNotBlank(c.getTelephone())) {
//                        errorInfo += "手机号未填";
//                    }
                    //流向
                    boolean flag = true;
                    if (orderNo.startsWith("TZ")){
                        if (!StrUtil.isNotBlank(c.getFlowName())) {
                            errorInfo += "流向未填";
                        }else{
                            for (SysDictVo sysDictVo : flowlist) {
                                if (sysDictVo.getName().equals(c.getFlowName())){
                                    flag = false;
                                    c.setFlow(sysDictVo.getId().intValue());
                                }
                            }
                            if (flag){
                                c.setFlow(dict.getId().intValue());
                            }
                        }
                    }

                    errorInfo += checkYuyue(c, orderNo, companyId);
                    c.setErrorInfo(errorInfo);
//                    // 填充 yuyueList 属性(可写可不写)
                    list.add(c);
                }
                build = ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return build;

    }

    private String checkYuyue(VehicleReservationVo vo, String orderNo, String companyId) {
        vo.setReservationType(orderNo.substring(0, 2));//业务类型
        //车牌校验
        if (vo.getCarNo() != null && !"".equals(vo.getCarNo())) {

            String patt = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$";
            Pattern r = Pattern.compile(patt);
            Matcher matcher = r.matcher(vo.getCarNo());
            if (!matcher.matches()) {
                return "车牌格式不对;";
            }

            //委托orderNo 所授权的公司
            SysAuth auth = sysAuthMapper.getSlaveByOrderNo(orderNo);

            //车牌是否为(当前公司+所授权的公司)下的
            CarInfoVo c = new CarInfoVo();
            c.setCompanyId(companyId);
            if (auth!=null){
                c.setSlaveComId(auth.getSlaveComId().toString());
            }

            List<CarInfo> list = carInfoMapper.getListByComIds(c);
            boolean flag = true;
            for (CarInfo info : list) {
                if (vo.getCarNo().equals(info.getCarNo())) {
                    flag = false;
                }
            }
            if (flag) {
                return "该车牌不属于当前客户公司下！;";
            }

            CarInfo car = carInfoService.getByCarNo(vo.getCarNo());
            if (car.getIsBlack() == 1) {
                return "该车为黑名单;";
            }

        }

        //


        //司机name校验   手机号校验
        if (vo.getDriverName() != null && !"".equals(vo.getDriverName()) && vo.getTelephone() != null && !"".equals(vo.getTelephone())) {
            LambdaQueryWrapper<UserMember> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserMember::getUserType, 2)
                    .eq(UserMember::getDisable, 0)
                    .eq(UserMember::getTelephone, vo.getTelephone())
                    .like(UserMember::getFleet, companyId);
            UserMember one = userMemberService.getOne(wrapper);
            if (one == null) {
                return "司机不属于此公司！;";
            } else {
                if (one.getIsBlack() == 1) {
                    return "司机为黑名单！;";
                }
                vo.setDriver(one.getId().intValue());
            }
        }

        //箱号校验(委托号下的箱号)
        LambdaQueryWrapper<YwOrderCntr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YwOrderCntr::getDisable, 0)
                .eq(YwOrderCntr::getOrderNo, orderNo)
                .eq(YwOrderCntr::getOrderFlag, 0);//未预约的
        List<YwOrderCntr> cntrList = ywOrderCntrMapper.selectList(wrapper);
        boolean flagA = true;//箱A标记
        boolean flagB = true;//箱B标记
        String cntrSizeA = "";//箱A尺寸
        String cntrSizeB = "";//箱B尺寸
        if (cntrList != null && cntrList.size() > 0) {
            for (YwOrderCntr cntr : cntrList) {

                if (cntr.getCntrNo().equals(vo.getCntrNo())) {
                    CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(vo.getCntrNo());
                    vo.setCntrIdA(ywOrderCntrMapper.getCntrByCntrNoAndOrderNo(vo.getCntrNo(), orderNo));
                    vo.setCntrSize(cntrInfo.getCntrSize());
                    cntrSizeA = cntrInfo.getCntrSize();
                    flagA = false;
                }
                if (vo.getCntrNoB() != null && !"".equals(vo.getCntrNoB())) {
                    if (cntr.getCntrNo().equals(vo.getCntrNoB())) {
                        CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(vo.getCntrNoB());
                        vo.setCntrIdB(ywOrderCntrMapper.getCntrByCntrNoAndOrderNo(vo.getCntrNoB(), orderNo));
                        vo.setCntrSizeB(cntrInfo.getCntrSize());
                        cntrSizeB = cntrInfo.getCntrSize();
                        flagB = false;
                    }
                }
            }
        } else {
            return "该委托下没有未预约的箱子了！;";
        }

        //表格箱A判断
        if (flagA) {
            return "该委托下没有箱号1！;";
        }

        //表格箱A判断
        if (vo.getCntrNoB() != null && !"".equals(vo.getCntrNoB())) {
            if (flagB) {
                return "该委托下没有箱号2！;";
            }
            //箱子尺寸校验
            if (cntrSizeA.substring(0, 2).equals(cntrSizeB.substring(0, 2))) {//尺寸相同
                if (cntrSizeA.startsWith("40")) {
                    return "两个箱子不能都为40尺！;";
                }


            } else {
                return "两个箱子尺寸不同！;";
            }

        }

        return "";
    }
}
